﻿Imports Microsoft.VisualBasic
Imports System
Imports System.Web
Imports System.Collections.Generic


Namespace Camstar.WebClient.HttpHandlers
    Public Class StreamFileHandler
        Implements IHttpHandler
        Implements IRequiresSessionState

        Public ReadOnly Property IsReusable() As Boolean Implements System.Web.IHttpHandler.IsReusable
            Get
                Return True
            End Get
        End Property

        Public Sub ProcessRequest(ByVal context As System.Web.HttpContext) Implements System.Web.IHttpHandler.ProcessRequest



            Dim params() As String
            If Not context.Session(Camstar.WebClient.WebConstants.SessionConstants.DocumentViewFile) Is Nothing Then
                params = context.Session(Camstar.WebClient.WebConstants.SessionConstants.DocumentViewFile)
            Else
                ReDim params(1)
                params(0) = context.Request.QueryString("fullPath")
                params(0) = System.Text.Encoding.Unicode.GetString(Convert.FromBase64String(params(0)))

                params(1) = context.Request.QueryString("fileName")
                params(1) = System.Text.Encoding.Unicode.GetString(Convert.FromBase64String(params(1)))

            End If

            context.Session.Remove(Camstar.WebClient.WebConstants.SessionConstants.DocumentViewFile)
            Dim configFolder As String = Camstar.WebClient.WebUtility.WebConfigUtil.GetValue(Camstar.WebClient.WebConstants.WebConfigConstants.DefaultSettings, Camstar.WebClient.WebConstants.WebConfigKeyConstants.TemporaryDirectory)
            Dim fullPath As String = params(0)
            context.Response.AddHeader("Content-Disposition", "attachment; filename=" + params(1))
            context.Response.WriteFile(fullPath)
            context.Response.End()
            Try
                System.IO.File.Delete(fullPath)
            Catch
            End Try
        End Sub
    End Class
End Namespace
